package com.foreknow.service;

import java.sql.SQLException;
import java.util.List;

import com.foreknow.dao.impl.BlogDaoImpl;
import com.foreknow.entity.Blog;

public class BlogService {
    private BlogDaoImpl blogDaoImpl;

    public BlogService() {
        blogDaoImpl = new BlogDaoImpl();
    }

    public List<Blog> getBlogsByCondition(String condition) {
        condition = "%" + condition + "%";
        String sql = "select * from blog where title like ? or category like ? order by readingVolume DESC";
        try {
            return blogDaoImpl.queryByCondition(sql, condition, condition);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public List<Blog> getBlogsByUserIdWithPaging(Integer userId,Integer limit,Integer offset){
        String sql = "select * from blog where userId = ? order by readingVolume DESC limit ?, ?";
        try {
            return blogDaoImpl.queryByCondition(sql, userId,limit,offset);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public List<Blog> getBlogsByConditionWithPaging(String condition,Integer limit,Integer offset){
        condition = "%" + condition + "%";
        String sql = "select * from blog where title like ? or category like ? order by readingVolume DESC limit ? , ?";
        try {
            return blogDaoImpl.queryByCondition(sql, condition, condition,limit,offset);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 随机获取10条博客
     *
     * @return
     */
    public List<Blog> getRandomBlogs(Integer limit,Integer offset) {
        String sql = "SELECT * FROM blog  AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM blog)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.readingVolume DESC LIMIT ?,?";
        try {
            return blogDaoImpl.queryByCondition(sql,limit,offset);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取用户的所有博客
     *
     * @param userId
     * @return
     */
    public List<Blog> getUserBlogs(Integer userId) {
        String sql = "select* from blog where userId = ? order by readingVolume DESC";
        try {
            return blogDaoImpl.queryByCondition(sql, userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据Id获取博客
     *
     * @param id
     * @return
     */
    public Blog getBlogById(Integer id) {
        String sql = "select * from blog where id = ?";
        try {
            return blogDaoImpl.queryOne(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 插入一条博客，成功返回id失败返回-1
     *
     * @param blog
     * @return
     */
    public Integer insertBlog(Blog blog) {
        String sql = "insert into blog(title,content,uploadTime,userId,category,introduction) value(?,?,?,?,?,?)";
        Object[] params = { blog.getTitle(), blog.getContent(), blog.getUploadTime(), blog.getUserId(),
                blog.getCategory(), blog.getIntroduction() };
        try {
            return blogDaoImpl.insert(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 根据Blog的ID删除Blog
     *
     * @param id
     * @return
     */
    public Boolean deleteBlogById(Integer id) {
        String sql = "delete from blog where id = ?";
        try {
            return blogDaoImpl.delete(sql, id) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改Blog，只能修改标题、内容和分类
     *
     * @param blog
     * @return
     */
    public Boolean updateBlog(Blog blog) {
        String sql = "update blog set title = ? ,content = ? , uploadTime = ? ,category = ? where id = ?";
        try {
            return blogDaoImpl.update(sql, blog.getTitle(), blog.getContent(), blog.getUploadTime(), blog.getCategory(),
                    blog.getId()) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更改Blog 赞数
     *
     * @param id
     * @param
     * @return
     */
    public Boolean addBlogReadingVolume(Integer id, Integer volume) {
        String sql = "update blog set readingVolume = ? where id = ?";
        try {
            return blogDaoImpl.update(sql, volume, id) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Integer getBlogReadingVolume(Integer id) {
        String sql = "select * from blog where id = ?";
        try {
            return blogDaoImpl.queryOne(sql, id).getReadingVolume();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
